import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import eslintPlugin from 'vite-plugin-eslint' //导入包
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

import path from 'path'
import fs from 'fs'

// https://vitejs.dev/config/
export default defineConfig((mode, command) => {
    // 解决终端optimized dependencies changed. reloading问题(https://www.cnblogs.com/ljyn/p/17340128.html)
    const optimizeDepsElementPlusIncludes = ['element-plus/es', '@vueuse/core']
    fs.readdirSync('node_modules/element-plus/es/components').map((dirname) => {
        fs.access(`node_modules/element-plus/es/components/${dirname}/style/css.mjs`, (err) => {
            if (!err) {
                optimizeDepsElementPlusIncludes.push(`element-plus/es/components/${dirname}/style/css`)
            }
        })
    })

    return {
        optimizeDeps: {
            include: optimizeDepsElementPlusIncludes,
        },
        resolve: {
            alias: {
                '~': path.resolve(__dirname, 'src'),
            },
        },
        server: {
            proxy: {
                '/api': {
                    target: 'http://ceshi13.dishait.cn',
                    // target: 'http://sceshi14.dishait.cn',
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/api/, ''),
                },
            },
        },
        plugins: [
            vue(),
            // 增加下面的配置项,这样在运行时就能检查eslint规范
            eslintPlugin({
                include: ['src/**/*.js', 'src/**/*.vue', 'src/*.js', 'src/*.vue'],
            }),
            /* elementplus按需导入 */
            AutoImport({
                resolvers: [ElementPlusResolver()],
                imports: ['vue', 'vue-router', 'pinia', '@vueuse/core'],
                dts: 'src/auto-import.d.ts',
                eslintrc: {
                    enabled: true, // Default `false`
                    filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
                    globalsPropValue: true, // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
                },
            }),
            Components({
                resolvers: [ElementPlusResolver()],
            }),
        ],
        build: {
            outDir: 'dist',
            minify: 'esbuild',
            // esbuild 打包更快，但是不能去除 console.log，terser打包慢，但能去除 console.log
            // minify: "terser",
            // terserOptions: {
            // 	compress: {
            // 		drop_console: viteEnv.VITE_DROP_CONSOLE,
            // 		drop_debugger: true
            // 	}
            // },
            // 禁用 gzip 压缩大小报告，可略微减少打包时间
            reportCompressedSize: false,
            // 规定触发警告的 chunk 大小
            chunkSizeWarningLimit: 1500,
            rollupOptions: {
                output: {
                    // Static resource classification and packaging
                    chunkFileNames: 'assets/js/[name]-[hash].js',
                    entryFileNames: 'assets/js/[name]-[hash].js',
                    assetFileNames: 'assets/[ext]/[name]-[hash].[ext]',
                },
            },
        },
    }
})
